<?php

namespace Admin\Controller;

/**
 * 海报控制器
 * @author wangyuanlin
 * 2015-05-17
 */
class AppposterController extends AdminController {

    /**
     * 海报列表
     * @author wangyuanlin
     * 2015-05-17
     */
    public function index() {
        $title = I('title');
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('app_poster', $map ,'createdate desc');
        int_to_string($list);  
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    /**
     * 新增海报
     * @author zhangwei
     * 2015-07-2
     */
    public function add($title = "", $urlstr = "",$mold=null,$pid=0) {
        if (IS_POST) {
            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();

            if (!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            } else {// 上传成功
                $data = array('title' => $title,'mold' => $mold,'urlstr' => $urlstr,'pid' => $pid, 'fileurl' => $info["photo"]["url"], 'createdate' => NOW_TIME, 'lastupdate' => NOW_TIME);
                $id = M('app_poster')->add($data);
                if (!$id) {
                    $this->error("添加失败！");
                } else {
                    $this->success('添加成功！', U('index'));
                }
            }
        } else {
            $this->display();
        }
    }
    
    /**
     * 修改海报
     * @author wangyuanlin
     * 2015-05-17
     */
    public function edit($title = "", $urlstr = "",$mold=null,$pid=0) {
        $id = I('id');
        if (IS_POST) {
            
            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();
            
            if($info){
                $data = array('title' => $title,'mold' => $mold, 'fileurl' => $info["photo"]["url"], 'urlstr' => $urlstr,'pid' => $pid, 'lastupdate' => NOW_TIME);
            }else{
                $data = array('title' => $title,'mold' => $mold,'urlstr' => $urlstr,'pid' => $pid, 'lastupdate' => NOW_TIME);
            }
            
            if (!D('app_poster')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('index'));
            }
        } else {
            $data = M("app_poster")->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    /**
     * 删除海报
     * @author zhangwei
     * 2015-07-2
     */
    public function del($id = 0) {
        if ($id) {
            $Poster = M("app_poster");
            
            $Poster->status = -1;
            $reuslt = $Poster->where(array('id'=>$id))->save(); 
            
            if ($reuslt) {
                $this->success('删除成功！', U('index'));
            } else {
                $this->error("删除失败！");
            }
        }
    }

    public function appadvice(){
        $map['status']=1;
        $list = $this->lists('app_advice', $map ,'createAt desc');
        for($i=0;$i<count($list);$i++){
            $map['id']=$list[$i]['uid'];
            $res=M('buyers')->where($map)->find();
            $list[$i]['realName']=$res['realName'];
            $list[$i]['phone']=$res['phone'];
            $list[$i]['province']=$res['province'];
            $list[$i]['city']=$res['city'];
            $list[$i]['street']=$res['street'];
            $list[$i]['shopName']=$res['shopName'];
        }
        $this->assign('_list',$list);
        $this->display();
    }

    public function usermanagement($phone='',$realName='',$shopName='',$street='',$status=''){
//        $map['source']=2;
        if ($phone) {
            $map['phone'] = array('like','%'.$phone.'%');
        }
        if ($realName) {
            $map['realName'] = array('like','%'.$realName.'%');
        }
        if ($shopName) {
            $map['shopName'] = array('like','%'.$shopName.'%');
        }
        if ($street) {
            $map['street'] = array('like','%'.$street.'%');
        }
        if ($status) {
            if($status == '2'){
                $map['status'] = 2;
            }elseif($status == '5'){
                $map['status'] = array('neq',2);
            }
        }
        $res=$this->lists('buyers',$map,'createAt desc');
        for($i=0;$i<count($res);$i++){
            if($res[$i]['status']==1){
                $res[$i]['statusname']='已审核';
            }elseif($res[$i]['status']==2){
                $res[$i]['statusname']='审核中';
            }elseif($res[$i]['status']==0){
                $res[$i]['statusname']='失效';
            }
        }
        $this->status=I('status');
        $this->assign('_list',$res);
        $this->display();
    }

    public function userdetail(){
        $map['id']=I('id');
        $aaid=I('id');
        if(IS_POST){
            $data['status']=I('status');
            $data['remark']=I('remark');
            $data['reviewer']=I('reviewer');
            $data['reviewtime']=NOW_TIME;

            $model = M('buyers');
            $id = $model->where($map)->save($data);
            if (!$id) {
                $this->error("操作失败！");
            } else {
                $this->success('保存成功！', U('nextuserdetail?id='.$aaid));
            }
        }else{
            $data=M('buyers')->where($map)->find();
            $this->assign('data',$data);

            $mapr['source']=$data['source'];
            $mapr['status']=array('neq','2');
            $resr=M('buyers')->where($mapr)->select();
            $sumr=count($resr);
            $this->assign('sumr',$sumr);

            $mapn['source']=$data['source'];
            $mapn['status']=array('eq','2');
            $resn=M('buyers')->where($mapn)->select();
            $sumn=count($resn);
            $this->assign('sumn',$sumn);

            $this->display();
        }
    }

    public function nextuserdetail(){
        $map['id']=I('id');
        $res=M('buyers')->where($map)->find();
        $mapa['id']=array('gt',I('id'));
        $mapa['status']=2;
        $resa=M('buyers')->where($mapa)->limit(1)->select();
        if(!$resa) {
            $this->error("下面已经没有了！", U('usermanagement'));
        }else{
            $mapr['source']=$res['source'];
            $mapr['status']=array('neq','2');
            $resr=M('buyers')->where($mapr)->select();
            $sumr=count($resr);
            $this->assign('sumr',$sumr);

            $mapn['source']=$res['source'];
            $mapn['status']=array('eq','2');
            $resn=M('buyers')->where($mapn)->select();
            $sumn=count($resn);
            $this->assign('sumn',$sumn);

            $this->assign('data',$resa[0]);
            $this->display();
        }
    }

}
